package org.zuma.gradle.springboot.plugins.oci;

import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.jetbrains.annotations.NotNull;
import org.springframework.boot.gradle.dsl.SpringBootExtension;
import org.springframework.boot.gradle.plugin.ResolveMainClassName;
import org.springframework.boot.gradle.tasks.bundling.BootJar;
import org.zuma.gradle.springboot.SpringBootBomAction;
import org.zuma.gradle.springboot.task.oci.docker.DockerTaskRegister;

import java.util.function.Supplier;

public class SpringBootOciPlugin implements Plugin<Project> {
    @Override
    public void apply(@NotNull Project project) {
        SpringBootBomAction.config(project);
        SpringBootBomAction.enableLombok(project);
        SpringBootBomAction.enableConfigProcessor(project);
        SpringBootBomAction.enableDevTools(project);
        SpringBootBomAction.enableSpringJunitTest(project);
        SpringBootBomAction.enableSpringCloud(project, "2025.0.0");
        var bootJar = project.getTasks().named("bootJar", BootJar.class);
        Supplier<Boolean> enableFunc = () -> {
            return project.getExtensions().getByType(SpringBootExtension.class).getMainClass().getOrNull() != null;
        };
        bootJar.configure(task -> {
            task.onlyIf(t -> enableFunc.get());
        });
        var resolveMainClassTask = project.getTasks().named("resolveMainClassName", ResolveMainClassName.class);
        resolveMainClassTask.configure(task -> {
            task.onlyIf(t -> enableFunc.get());
        });
        DockerTaskRegister.registry(project, bootJar, enableFunc);
    }

}
